package com.yan.demo;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author taoge
 * @Date 2024/8/14 9:29
 * @Description
 */
public class ReflectTest1 {

    @Test
    public void test1() throws ClassNotFoundException {
        Class aClass = Class.forName("com.yan.demo.Student");
        System.out.println("aClass = " + aClass);

        Class aClass1 = Student.class;
        System.out.println("aClass1 = " + aClass1);

        Class aClass2 = new Student().getClass();
        System.out.println("aClass2 = " + aClass2);
    }

    @Test
    public void test2() throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class<?> aClass = Class.forName("com.yan.demo.Student");
        Field name = aClass.getDeclaredField("name");
        System.out.println("name = " + name);
        String simpleName = aClass.getSimpleName();
        System.out.println("simpleName = " + simpleName);
        Method setName = aClass.getDeclaredMethod("setName", String.class);
        System.out.println("setName = " + setName);
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }

        int modifiers = name.getModifiers();
        System.out.println("modifiers = " + modifiers);

    }
}
